
#include "StateMainMenu.h"
#include "ProgressManager.h"
#include "InputManager.h"
#include "StateManager.h"
#include "StateGame.h"
#include "StateOptions.h"
#include "StateStats.h"
#include "StateMuseum.h"
//#include "StateShop.h"
#include "StateShopAnimals.h"
#include "defs.h"
#include "MusicManager.h"
#include "RumbleEffect.h"
#include "hRandom.h"
#include "BackgroundManager.h"

StateMainMenu::StateMainMenu()
{
}

StateMainMenu::~StateMainMenu()
{
}

void StateMainMenu::loadContent()
{
    m_oMainMenu = new Container();

    m_oGroundGroup1 = new Container();
    m_oGroundGroup2 = new Container();
    m_oGroundGroup3 = new Container();
    
    m_oSkyGroup1 = new Container();
    m_oSkyGroup2 = new Container();
    m_oSkyGroup3 = new Container();
    
    m_oTitle = new Container();
    m_oButtons = new Container();
    
    
    m_oBg = new RenderableEntity("bg", Vector2(-34, -346));

    //Sky
    m_oCloud1 = new RenderableEntity("cloud1", Vector2(500, 550));
    m_oCloud1->setCentered();
    m_oCloud2 = new RenderableEntity("cloud2", Vector2(200, 700));
    m_oCloud2->setCentered();
    m_oCloud3 = new RenderableEntity("cloud3", Vector2(400, 800));
    m_oCloud3->setCentered();
    m_oMinicloud = new RenderableEntity("minicloud", Vector2(60, 920));
    m_oMinicloud->setCentered();
    m_oStork = new RenderableEntity("stork", Vector2(140, 420));
    m_oStork->setCentered();
    m_oFog = new RenderableEntity("fog", Vector2(600, 300));
    m_oFog->setCentered();

    m_oSkyGroup1->addEntity(m_oBg);
    m_oSkyGroup1->addEntity(m_oMinicloud);
    m_oSkyGroup1->addEntity(m_oCloud1);
    m_oSkyGroup1->addEntity(m_oCloud3);
    m_oSkyGroup1->addEntity(m_oCloud2);
    //m_oSkyGroup1->addEntity(m_oStork);
    m_oSkyGroup1->addEntity(m_oFog);
    
    //m_oMainMenu->addEntity(m_oSkyGroup1);
    

    //Title
    m_oBglogo = new RenderableEntity("bglogo", Vector2(320, 750));
    m_oBglogo->setCentered();

    m_oHungry = new RenderableEntity("Title_Hungry", Vector2(0, 660));
    m_oHungry->setCenter(Vector2(1.0f, 0.0f));
    
    m_oBirths = new RenderableEntity("Title_Births", Vector2(640, 498));
    
    m_oChupeteGrande = new RenderableEntity("Title_Pacifier", Vector2(151, 674));
    m_oChupeteGrande->setCentered();
    m_oChupeteGrande->setAlpha(0);
    
    m_oTitle->addEntity(m_oBglogo);
    m_oTitle->addEntity(m_oHungry);
    m_oTitle->addEntity(m_oBirths);
    m_oTitle->addEntity(m_oChupeteGrande);
    
    m_oMainMenu->addEntity(m_oTitle);
    

    //Buttons
    m_oHuevete = new RenderableEntity("huevete", Vector2(230, 360));
    m_oHuevete->setCentered();
    m_oHuevete->setAlpha(0);

    m_oPlay = new RenderableEntity("play", Vector2(230, 400));
    m_oPlay->setCentered();
    m_oPlay->setAlpha(0);
    m_oPlay->setupButton(Vector2(0.5f, 0.5f));
    m_oPlay->changeColorWhenClicked(true);
    
    m_oOptions = new RenderableEntity("options", Vector2(230, 270));
    m_oOptions->setCentered();
    m_oOptions->setAlpha(0);
    m_oOptions->setupButton(Vector2(0.5f, 0.5f));
    m_oOptions->changeColorWhenClicked(true);
    
    m_oButtons->addEntity(m_oHuevete);
    m_oButtons->addEntity(m_oPlay);
    m_oButtons->addEntity(m_oOptions);
    
    m_oMainMenu->addEntity(m_oButtons);
    
    
    //Ground
    m_oGrassbg2 = new RenderableEntity("grassbg2", Vector2(280, 120));
    m_oGrassbg2->setCentered();
    m_oPedestal = new RenderableEntity("pedestal", Vector2(110, 230));
    m_oPedestal->setCentered();
    m_oChabolo = new RenderableEntity("chabolo", Vector2(770, 480));
    m_oChabolo->setCentered();
    m_oGround = new RenderableEntity("ground", Vector2(500, 50));
    m_oGround->setCentered();
    m_oShopsign = new RenderableEntity("shopsign", Vector2(500, 215));
    m_oShopsign->setCentered();
    m_oShopsign->setScale(Vector2(1.5f, 1.5f));
    m_oGrassforeground3 = new RenderableEntity("grassforeground3", Vector2(880, 30));
    m_oGrassforeground3->setCentered();
    m_oGrassmed = new RenderableEntity("grassmed", Vector2(480, 30));
    m_oGrassmed->setCentered();
    m_oGrassmed2 = new RenderableEntity("grassmed2", Vector2(80, 60));
    m_oGrassmed2->setCentered();
    m_oGrassforeground1 = new RenderableEntity("grassforeground1", Vector2(210, -10));
    m_oGrassforeground1->setCentered();
    m_oGrassbg1 = new RenderableEntity("grassbg1", Vector2(600, 150));
    m_oGrassbg1->setCentered();
    m_oMariquitis = new RenderableEntity("mariquitiquis", Vector2(490, 80));
    m_oMariquitis->setCentered();
    m_oBolet2 = new RenderableEntity("bolet2", Vector2(470, 0));
    m_oBolet2->setCentered();
    m_oBolet = new RenderableEntity("bolet", Vector2(610, 70));
    m_oBolet->setCentered();
    m_oGrassforeground2 = new RenderableEntity("grassforeground2", Vector2(600, -10));
    m_oGrassforeground2->setCentered();

    m_oPedestal->changeColorWhenClicked(true);
    m_oShopsign->changeColorWhenClicked(true);
    
    m_oGroundGroup1->addEntity(m_oGrassbg2);
    m_oGroundGroup1->addEntity(m_oPedestal);
    m_oGroundGroup1->addEntity(m_oChabolo);
    m_oGroundGroup1->addEntity(m_oGround);
    m_oGroundGroup1->addEntity(m_oShopsign);
    m_oGroundGroup2->addEntity(m_oGrassforeground3);
    m_oGroundGroup2->addEntity(m_oGrassmed);
    m_oGroundGroup2->addEntity(m_oGrassmed2);
    m_oGroundGroup2->addEntity(m_oGrassforeground1);
    m_oGroundGroup2->addEntity(m_oGrassbg1);
    m_oGroundGroup3->addEntity(m_oMariquitis);
    m_oGroundGroup3->addEntity(m_oBolet2);
    m_oGroundGroup3->addEntity(m_oBolet);
    m_oGroundGroup3->addEntity(m_oGrassforeground2);
    
    m_oMainMenu->addEntity(m_oGroundGroup1);
    m_oMainMenu->addEntity(m_oGroundGroup2);
    m_oMainMenu->addEntity(m_oGroundGroup3);
    
    //m_oMainMenu->setPosition(Vector2(-500, 0));
    
    
    //Enter effect
    m_eHungryEnter = new TimeLineEffect(false, false);
    m_eHungryEnter->addEffect(new MoveEffect(m_oGroundGroup1, 0.5f, false, false, Vector2(0, -300), Vector2(0, 0), EASE_OUT), 0.0f);
    m_eHungryEnter->addEffect(new MoveEffect(m_oGroundGroup2, 0.5f, false, false, Vector2(0, -600), Vector2(0, 0), EASE_OUT), 0.0f);
    m_eHungryEnter->addEffect(new MoveEffect(m_oGroundGroup3, 0.5f, false, false, Vector2(0, -900), Vector2(0, 0), EASE_OUT), 0.0f);
    m_eHungryEnter->addEffect(new MoveEffect(m_oSkyGroup1, 0.5f, false, false, Vector2(0, -40), Vector2(0, 0), EASE_OUT), 0.0f);

    m_eHungryEnter->addEffect(new MoveEffect(m_oHungry, 0.2, false, false, Vector2(0, 610), Vector2(560, 660)), 0);
    m_eHungryEnter->addEffect(new ScaleEffect(m_oHungry, 0.1, false, false, Vector2(1, 1), Vector2(0.8, 1.2)), 0.2);
    m_eHungryEnter->addEffect(new ScaleEffect(m_oHungry, 0.1, false, false, Vector2(0.8, 1.2), Vector2(1.1, 0.9)), 0.3);
    m_eHungryEnter->addEffect(new ScaleEffect(m_oHungry, 0.05, false, false, Vector2(1.1, 0.9), Vector2(1.0, 1.0)), 0.4);
    m_eHungryEnter->addEffect(new MoveEffect(m_oBirths, 0.2, false, false, Vector2(640, 448), Vector2(110, 498)), 0.2);
    m_eHungryEnter->addEffect(new ScaleEffect(m_oBirths, 0.1, false, false, Vector2(1, 1), Vector2(0.8, 1.2)), 0.4);
    m_eHungryEnter->addEffect(new ScaleEffect(m_oBirths, 0.1, false, false, Vector2(0.8, 1.2), Vector2(1.1, 0.9)), 0.5);
    m_eHungryEnter->addEffect(new ScaleEffect(m_oBirths, 0.05, false, false, Vector2(1.1, 0.9), Vector2(1.0, 1.0)), 0.6);
    m_eHungryEnter->addEffect(new FadeEffect(m_oChupeteGrande, 0.2, false, false, 0, 1), 0.5);
    m_eHungryEnter->addEffect(new FadeEffect(m_oHuevete, 0.1, false, false, 0, 1), 0.45);
    m_eHungryEnter->addEffect(new ScaleEffect(m_oHuevete, 0.1, false, false, Vector2(0, 0), Vector2(1.1, 1.1)), 0.45);
    m_eHungryEnter->addEffect(new ScaleEffect(m_oHuevete, 0.02, false, false, Vector2(1.1, 1.1), Vector2(1, 1)), 0.55);
    m_eHungryEnter->addEffect(new FadeEffect(m_oPlay, 0.1, false, false, 0, 1), 0.5);
    m_eHungryEnter->addEffect(new ScaleEffect(m_oPlay, 0.1, false, false, Vector2(0, 0), Vector2(1.1, 1.1)), 0.5);
    m_eHungryEnter->addEffect(new ScaleEffect(m_oPlay, 0.02, false, false, Vector2(1.1, 1.1), Vector2(1, 1)), 0.6);
    m_eHungryEnter->addEffect(new FadeEffect(m_oOptions, 0.1, false, false, 0, 1), 0.55);
    m_eHungryEnter->addEffect(new ScaleEffect(m_oOptions, 0.1, false, false, Vector2(0, 0), Vector2(1.1, 1.1)), 0.55);
    m_eHungryEnter->addEffect(new ScaleEffect(m_oOptions, 0.02, false, false, Vector2(1.1, 1.1), Vector2(1, 1)), 0.65);
    
    //Exit effect
    m_eHungryExit = new TimeLineEffect(false, false);
    m_eHungryExit->addEffect(new FadeEffect(m_oHungry, 0.2f, false, false, 1.0f, 0.0f), 0.0f);
    m_eHungryExit->addEffect(new FadeEffect(m_oBirths, 0.2f, false, false, 1.0f, 0.0f), 0.0f);
    m_eHungryExit->addEffect(new FadeEffect(m_oChupeteGrande, 0.2f, false, false, 1.0f, 0.0f), 0.0f);
    m_eHungryExit->addEffect(new FadeEffect(m_oHuevete, 0.2f, false, false, 1.0f, 0.0f), 0.0f);
    m_eHungryExit->addEffect(new FadeEffect(m_oPlay, 0.2f, false, false, 1.0f, 0.0f), 0.0f);
    m_eHungryExit->addEffect(new FadeEffect(m_oOptions, 0.2f, false, false, 1.0f, 0.0f), 0.0f);
    
    //StartGame effect
    m_oStartGame = new TimeLineEffect(false, false);
    m_oStartGame->addEffect(new MoveEffect(m_oGroundGroup1, 0.4f, false, false, Vector2(0.0f, -300.0f), EASE_IN), 0.0f);
    m_oStartGame->addEffect(new MoveEffect(m_oGroundGroup2, 0.4f, false, false, Vector2(0.0f, -600.0f), EASE_IN), 0.0f);
    m_oStartGame->addEffect(new MoveEffect(m_oGroundGroup3, 0.4f, false, false, Vector2(0.0f, -900.0f), EASE_IN), 0.0f);
    m_oStartGame->addEffect(new MoveEffect(m_oSkyGroup1, 0.4f, false, false, Vector2(0.0f, -40), EASE_IN), 0.0f);
    m_oStartGame->addEffect(new MoveEffect(m_oTitle, 0.4f, false, false, Vector2(0.0f, -200), EASE_IN), 0.0f);
    m_oStartGame->addEffect(new ScaleEffect(m_oHuevete, 0.02f, false, false, Vector2(1.0f, 1.0f), Vector2(1.1f, 1.1f)), 0.0f);
    m_oStartGame->addEffect(new FadeEffect(m_oBglogo, 0.2f, false, false, 1.0f, 0.0f), 0.0f);
    m_oStartGame->addEffect(new FadeEffect(m_oHungry, 0.2f, false, false, 1.0f, 0.0f), 0.0f);
    m_oStartGame->addEffect(new FadeEffect(m_oBirths, 0.2f, false, false, 1.0f, 0.0f), 0.0f);
    m_oStartGame->addEffect(new FadeEffect(m_oChupeteGrande, 0.2f, false, false, 1.0f, 0.0f), 0.0f);
    m_oStartGame->addEffect(new ScaleEffect(m_oHuevete, 0.10f, false, false, Vector2(1.1f, 1.1f), Vector2(0.0f, 0.0f)), 0.02f);
    m_oStartGame->addEffect(new ScaleEffect(m_oPlay, 0.02f, false, false, Vector2(1.0f, 1.0f), Vector2(1.1f, 1.1f)), 0.0f);
    m_oStartGame->addEffect(new ScaleEffect(m_oPlay, 0.10f, false, false, Vector2(1.1f, 1.1f), Vector2(0.0f, 0.0f)), 0.02f);
    m_oStartGame->addEffect(new ScaleEffect(m_oOptions, 0.02f, false, false, Vector2(1.0f, 1.0f), Vector2(1.1f, 1.1f)), 0.0f);
    m_oStartGame->addEffect(new ScaleEffect(m_oOptions, 0.10f, false, false, Vector2(1.1f, 1.1f), Vector2(0.0f, 0.0f)), 0.02f);
}

void StateMainMenu::unloadContent()
{
}

void StateMainMenu::enter()
{
    m_oTitle->setPosition(Vector2(0, 0));
    m_oHungry->setPosition(Vector2(0, 660));
    m_oBirths->setPosition(Vector2(640, 498));

    m_oBglogo->setAlpha(1.0f);
    m_oTitle->setAlpha(1.0f);
    m_oHungry->setAlpha(1.0f);
    m_oBirths->setAlpha(1.0f);
    m_oChupeteGrande->setAlpha(0.0f);

    m_oHuevete->setAlpha(0.0f);
    m_oPlay->setAlpha(0.0f);
    m_oOptions->setAlpha(0.0f);
    
    m_eHungryEnter->start(true);
    m_eState = MENU_ENTER;
    
    MusicManager::instance().playMusic();
}

void StateMainMenu::update(float dt)
{
    m_fTimeInState += dt;
    
    switch(m_eState)
    {
        case MENU_ENTER:
            if(m_eHungryEnter->isFinished())
            {
                m_eState = MENU_IDLE;
            }
            break;

        case MENU_IDLE:

            m_oPlay->update();
            m_oOptions->update();
            //m_oPedestal->update();
            m_oShopsign->update();
                        
            if(m_oPlay->isClicked())
            {
                m_eButtonSelected = m_oPlay;
                m_eState = MENU_EXIT;
                
                m_oStartGame->start(true);
                BackgroundManager::instance().fadeIn();
                
                //StateManager::instance().fadeOut();
                m_oPlay->setColor(COLOR_WHITE);
                m_fTimeInState = 0;
            }
            else if(m_oOptions->isClicked())
            {
                m_eButtonSelected = m_oOptions;
                m_eState = MENU_EXIT;
                
                m_oOptions->setColor(COLOR_WHITE);
                m_fTimeInState = 0;
                
                //m_eHungryExit->start(true);
            }
            /*
            else if(m_oPedestal->isClicked())
            {
                m_eState = MENU_EXIT;
                m_eButtonSelected = m_oPedestal;
            }
            */
            else if(m_oShopsign->isClicked())
            {
                m_eState = MENU_EXIT;
                m_eButtonSelected = m_oShopsign;
            }
            break;
        
        case MENU_EXIT:
            if(m_eButtonSelected == m_oPlay)
            {
                BackgroundManager::instance().update(dt);
                
                if(ProgressManager::instance().isMusicOn())
                {
                    MusicManager::instance().setMusicVolume(MUSIC_VOLUME - MUSIC_VOLUME * m_fTimeInState / FADE_TIME);
                }
                
                if(m_oStartGame->isFinished())
                {
                    MusicManager::instance().stopMusic();
                    
                    //StateManager::instance().fadeIn();
                    StateManager::instance().setState(StateGame::instance());
                }
            }
            else if(m_eButtonSelected == m_oOptions && m_eHungryExit->isFinished())
            {
                StateManager::instance().setState(StateOptions::instance());
            }
            else if(m_eButtonSelected == m_oPedestal && m_eHungryExit->isFinished())
            {
                StateManager::instance().setState(StateMuseum::instance());
            }
            else if(m_eButtonSelected == m_oShopsign && m_eHungryExit->isFinished())
            {
                //StateManager::instance().setState(StateShop::instance());
                StateManager::instance().setState(StateShopAnimals::instance());
            }
            break;
    }
}

void StateMainMenu::render()
{
    m_oSkyGroup1->render();
    
    BackgroundManager::instance().render();
    
    m_oMainMenu->render();
}

void StateMainMenu::exit()
{
}
